package com.qinghe.plant.util;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

public class UniCodeUtil implements Serializable {
	private static final long serialVersionUID = -5628028113003022356L;
	private static final int MAX_SIZE = 25;
	private static final int MIN_SIZE = 14;
	private static final int BASE_SIZE = 2;

	// 默认生成RCID 12位
	public static String rand() {
		return rand(16);
	}

	// 根据传入值生成相应位数随机码 , offset 最大生成40位，最小生成14位 (位权基础系数为4) 4 6 8 1 2 3
	public static String rand(int offset) {
		if (offset > MAX_SIZE || offset < MIN_SIZE) {
			return "";
		}
		int div = offset / 10;
		int bitSize = BASE_SIZE + (div << 2 >> 1);
		int baseSize = offset - bitSize;
		String baseID = randomBaseID(baseSize);
		StringBuilder rcid = new StringBuilder(baseID);
		String rv = bitWeight(bitSize);
		char[] ch = rv.toCharArray();
		for (char c : ch) {
			rcid.insert(Character.getNumericValue(c), c);
		}
		return rcid.toString().toUpperCase();
	}

	// 根据UUID生成随机指定位数位ID
	public static String randomBaseID(int baseSize) {
		UUID uid = UUID.randomUUID();
		long idx = uid.getLeastSignificantBits();
		StringBuilder buff = new StringBuilder();
		for (int i = 0; i < 12; i++) {
			buff.append(toa(0x1F & idx));
			idx >>>= 5;
		}
		long idxb = uid.getMostSignificantBits();
		buff.append(toa((idxb & 0x1) == 0 ? idx : idx + 0x10));
		idx = (idxb >>> 1);
		for (int i = 0; i < 13; i++) {
			buff.append(toa(0x1F & idx));
			idx >>>= 5;
		}
		return buff.reverse().toString().substring(0, baseSize);
	}

	// 获取位权数
	private static String bitWeight(int bitSize) {
		String str = String.valueOf(Math.random()).substring(2);
		while (str.length() < bitSize || str.indexOf('-') > 0) {
			str = String.valueOf(Math.random()).substring(2);
		}
		return str.substring(0, bitSize);
	}

	// 生成可见字符
	private static char toa(long lidx) {
		if (lidx < 10) {
			return (char) (0x30 + lidx);
		}
		lidx += (0x41 - 10);
		int[] skips = { 0x49, 0x4f, 0x57, 0x5a };
		for (int ch : skips) {
			if (lidx < ch) {
				break;}
			lidx++;
		}
		return (char) lidx;
	}

	// 获取包括基类的所有属性
	public static Field[] GetFields(Object object) {
		Field[] fields = object.getClass().getDeclaredFields();
		System.out.println(fields.length);
		Class<?> superclass = object.getClass().getSuperclass();
		Field[] superFields = superclass.getDeclaredFields();
		System.out.println(superFields.length);

		int length1 = fields.length;
		int length2 = superFields.length;

		fields = Arrays.copyOf(fields, length1 + length2);// 数组扩容
		System.arraycopy(superclass, 0, fields, length1, length2);
		System.out.println(Arrays.toString(fields));
		return fields;
	}
	
    public static Date GetLastDate()
    {
    	Date now = new Date();
        Date last=new Date(now.getTime()-90*24*60*60*1000); 
 	   return  last;
 	  //SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
 	  // System.out.println("今天的日期："+df.format(d));   
 	  // System.out.println("两天前的日期：" + df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000)));  
 	  // System.out.println("三天后的日期：" + df.format(new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000)));
    }
}
